package com.wx.frame.controller;

import com.alibaba.fastjson.JSON;
import com.wx.frame.model.Log;
import com.wx.frame.service.LogService;
import com.wx.utils.Page;
import com.wx.utils.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by myd on 16/3/4.
 */
@Controller("logController")
@RequestMapping("/log")
public class LogController {

    /**
     * 注入service
     */
    @Resource(name = "logServiceImpl")
    private LogService<Log> logService;

    /**
     * 分页获取数据列表
     * @param pageable
     * @param log
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Page<Log> list(Pageable pageable,Log log)
    {
        return logService.findPage(pageable, log);
    }

    /**
     * 保存信息
     * @param datas
     */
    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public void save(String datas)
    {
        try {
            List<Log> logs = JSON.parseArray("["+datas+"]",Log.class);
            for (Log log : logs)
            {
                this.logService.save(log);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    /**
     * 根据主键批量删除
     * @param ids 主键集合,使用逗号分隔
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public void delete(String ids)
    {
        System.out.println("删除"+ids);
        try{
            String[] tmps = ids.split(",");
            if(null != tmps && tmps.length > 0) {
                List<String> idList = new ArrayList<String>();
                for(String s: tmps) {
                    idList.add(s);
                }

                this.logService.delete(idList);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
